import express from "express";
import NodeService from "../../services/NodeService";
import ResponseHelper from "../ResponseHelper";

var router = express.Router()

router.get('/findByPage', async (req, res, next) => {
  const page = req.query.page || 1
  const limit = req.query.limit || 10

  try {
    const result = await NodeService.findByPage(+page, +limit, {})
    return ResponseHelper.send(result, res)
  } catch (err) {
    next()
  }
})

router.post('/', async (req, res, next) => {

  const data = req.body || {}

  try {
    const result = await NodeService.create(data)
    return ResponseHelper.send(result, res)
  } catch (err) {
    console.log(err);

    next()
  }
})

router.delete('/:id', async (req, res, next) => {
  const id = req.params.id || ''

  const result = await NodeService.del(id)
  ResponseHelper.send(result, res)

})

router.put('/:id', async (req, res, next) => {
  const id = req.params.id || ''
  const body = req.body || {}

  const result = await NodeService.put(id, body)
  ResponseHelper.send(result, res)

})


export default router